using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel.Configuration;
using System.Xml.Serialization;

namespace Spike.ContractBuilder
{
    [Serializable]
    public class ClientEndpoint : IXmlSerializable
    {
        public ClientEndpoint()
        {
            _assemblies = new List<string>();
        }

        public ClientEndpoint(ChannelEndpointElement endpoint)
        {
            _assemblies = new List<string>();
            _contract = endpoint.Contract;
            _name = endpoint.Name;
            _uri = endpoint.Address.AbsoluteUri;
        }

        private string _uri;

        public string URI
        {
            get { return _uri; }
            set { _uri = value; }
        }
	


        private string _contract;

        public string Contract
        {
            get { return _contract; }
            set { _contract = value; }
        }

        private string _name;

        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        private List<string> _assemblies;

        public List<string> Assemblies
        {
            get { return _assemblies; }
            set { _assemblies = value; }
        }

        #region IXmlSerializable Members

        public System.Xml.Schema.XmlSchema GetSchema()
        {
            return null;
        }

        public void ReadXml(System.Xml.XmlReader reader)
        {
            reader.ReadStartElement("ClientEndpoint");
            _name = reader.ReadElementString("Name");
            _contract = reader.ReadElementString("Contract");

            reader.ReadStartElement("Assemblies");
            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
            {
                _assemblies.Add(reader.ReadElementString("Assembly"));
            }
            reader.ReadEndElement();
            reader.ReadEndElement(); 
        }

        public void WriteXml(System.Xml.XmlWriter writer)
        {
            //writer.WriteStartElement("ClientEndpoint");
            writer.WriteElementString("Name", _name);
            writer.WriteElementString("Contract", _contract);

            writer.WriteStartElement("Assemblies");
            foreach (string assembly in _assemblies)
            {
                writer.WriteElementString("Assembly", assembly);
            }
            writer.WriteEndElement();
            //writer.WriteEndElement();
        }

        #endregion
    }
}
